/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.excesys.exsecs.om.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.om.service.OmMintenancePlanService;
import com.excesys.exsecs.om.service.OmMintenancePlanTimesService;
import com.excesys.exsecs.om.vo.MaintenancePlanExcelVo;
import com.excesys.exsecs.om.vo.MaintenancePlanVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 保养计划
 *
 * @author
 * @date 2023-08-22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ommintenanceplan")
@Tag(name = "保养计划")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class OmMaintenancePlanController {

	private final OmMintenancePlanService omMintenancePlanService;

	private final OmMintenancePlanTimesService omMintenancePlanTimesService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param maintenancePlanVo 保养计划
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	// @PreAuthorize("@pms.hasPermission('om_omrepairplan_get')" )
	public R getOmmintenancePlanPage(Page page, MaintenancePlanVo maintenancePlanVo) {
		return R.ok(omMintenancePlanService.getMintenancePlanPage(page, maintenancePlanVo));
	}

	/**
	 * 新增保养计划
	 * @param maintenancePlanVo 新增保养计划
	 * @return R
	 */
	@Operation(summary = "新增保养计划", description = "新增保养计划")
	@SysLog("新增保养计划")
	@PostMapping
	// @PreAuthorize("@pms.hasPermission('om_omrepairplan_add')" )
	public R save(@RequestBody MaintenancePlanVo maintenancePlanVo) {
		return R.ok(omMintenancePlanService.saveMintenancePlan(maintenancePlanVo));
	}

	/**
	 * 通过id删除维修计划
	 * @param exId id
	 * @return R
	 */
	@Operation(summary = "通过id删除维修计划", description = "通过id删除维修计划")
	@SysLog("通过id删除维修模板")
	@DeleteMapping("/{exId}")
	// @PreAuthorize("@pms.hasPermission('om_omrepairplan_del')" )
	public R removeById(@PathVariable Integer exId) {
		omMintenancePlanTimesService.removeByParentId(exId + "");
		return R.ok(omMintenancePlanService.removeById(exId));
	}

	/**
	 * 修改维修计划
	 * @param maintenancePlanVo 修改维修计划
	 * @return R
	 */
	@Operation(summary = "修改维修计划", description = "修改维修计划")
	@SysLog("修改维修计划")
	@PutMapping
	// @PreAuthorize("@pms.hasPermission('om_omrepairplan_edit')" )
	public R updateById(@RequestBody MaintenancePlanVo maintenancePlanVo) {
		return R.ok(omMintenancePlanService.updatePlan(maintenancePlanVo));
	}

	/**
	 * 删除多个设备维修模板
	 * @param idList 删除多个设备维修模板
	 * @return R
	 */
	@Operation(summary = "删除多个设备维修模板", description = "删除多个设备维修模板")
	@SysLog("删除多个设备维修模板")
	@DeleteMapping("/removeByIds")
	// @PreAuthorize("@pms.hasPermission('om_omrepairplan_del')")
	public R removeByIds(@RequestBody List<Integer> idList) {
		for (int i = 0; i < idList.size(); i++) {
			omMintenancePlanTimesService.removeByParentId(idList.get(i) + "");
		}
		return R.ok(omMintenancePlanService.removeByIds(idList));
	}

	/**
	 * 导出excel 表格
	 * @return
	 */

	@Operation(summary = "导出所有数据", description = "导出所有数据")
	@ResponseExcel
	@GetMapping("/export")
	// @PreAuthorize("@pms.hasPermission('om_omicheckpoint_export')")
	public List<MaintenancePlanExcelVo> export() {
		return omMintenancePlanService.listBamSBuildings();
	}

	/**
	 * 执行
	 * @param maintenancePlanVo 执行
	 * @return R
	 */
	@Operation(summary = "执行", description = "执行")
	@SysLog("执行")
	@PostMapping("/execute")
	// @PreAuthorize("@pms.hasPermission('om_omrepairmodel_add')" )
	public R execute(@RequestBody MaintenancePlanVo maintenancePlanVo) {
		return R.ok(omMintenancePlanService.execute(maintenancePlanVo));
	}

}
